package com.pw.study.flink.sql;

import com.pw.study.flink.entities.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

import static org.apache.flink.table.api.Expressions.$;

public class $09SqlTime {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        mc1(tEnv);
        mc2(env, tEnv);
        mc3(tEnv);
        mc4(env,tEnv);



    }

    private static void mc4(StreamExecutionEnvironment env, StreamTableEnvironment tEnv) {
        DataStream<WaterSensor> stream = env
                .fromElements(
                        new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60)
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((ws, ts) -> ws.getTs())

                );



        // 1. 在转成表的时候, 添加一个新的字段作为事件时间
//        Table table = tEnv.fromDataStream(stream, $("id"), $("ts"), $("vc"), $("et").rowtime());
        // 2. 把现有字段转成事件事件
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts").rowtime(), $("vc"));
        table.printSchema();
        table.execute().print();
    }


    private static void mc3(StreamTableEnvironment tEnv) {
        tEnv.getConfig().setLocalTimeZone(ZoneOffset.ofHours(1));
        tEnv.executeSql("create table sensor(" +
                " id string," +
                " ts bigint," +
                " vc int," +
                " et as to_timestamp_ltz(ts,3)," +
                " watermark for et as et - interval '3' second ) with (" +
                " 'connector' = 'filesystem', " +
                " 'path' = 'data/file/sensor.txt'," +
                " 'format' = 'csv' ) ");
        tEnv.sqlQuery("select * from sensor").execute().print();

    }

    private static void mc2(StreamExecutionEnvironment env, StreamTableEnvironment tEnv) {
        // 1. 先创建一个流
        DataStreamSource<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1000L, 10),
                new WaterSensor("sensor_1", 2000L, 20),
                new WaterSensor("sensor_2", 3000L, 30),
                new WaterSensor("sensor_1", 4000L, 40),
                new WaterSensor("sensor_1", 5000L, 50),
                new WaterSensor("sensor_2", 6000L, 60)
        );
        Table table = tEnv.fromDataStream(stream, $("id"), $("ts"), $("pt").proctime(), $("vc"));
        table.execute().print();
    }

    private static void mc1(StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table tb1( id string, ts bigint,vc int," +
                " pt as proctime()) with ( 'connector'='filesystem', " +
                " 'path'='data/file/a.txt','format'='csv')");
        tEnv.sqlQuery("select * from tb1").execute().print();
    }

    private static void writeFile(StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table tb1( id string, ts bigint,vc int) with ( 'connector'='filesystem', " +
                " 'path'='data/file/b','format'='csv')");
        tEnv.sqlQuery("select * from sensor").executeInsert("tb1");
        tEnv.sqlQuery("select * from tb1 where id='sensor_1'").execute().print();
    }

    private static void readFile(StreamTableEnvironment tEnv) {
        tEnv.executeSql("create table sensor(" +
                " id string," +
                " ts bigint," +
                " vc int) with (" +
                " 'connector' = 'filesystem', " +
                " 'path' = 'data/file/sensor.txt'," +
                " 'format' = 'csv' ) ");

        tEnv.sqlQuery("select * from sensor where id='sensor_1'").execute().print();
    }


}
